﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Rich4Editor
{
    public partial class Form1 : Form
    {
        // 打开的文件
        private String openedFile;

        // 文件编辑器对象
        private FileEditor fe;

        // 编辑面板数组对象
        private EditPanel[] eps;

        // 构造函数
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体载入时调用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            // 删除默认的两个Tab
            tabControl1.Dock = DockStyle.Fill;
            tabControl1.TabPages.Clear();

            // 加载说明页
            IntroPanel p1 = new IntroPanel();

            p1.FormBorderStyle = FormBorderStyle.None;
            p1.TopLevel = false;
            p1.ControlBox = false;
            p1.Dock = DockStyle.Fill;
            p1.Show();

            TabPage page1 = new TabPage("程序说明");
            p1.Parent = page1;
            tabControl1.TabPages.Add(page1);


            toolStripStatusLabel1.Text = "请选择大富翁4存档文件...";
        }

        /// <summary>
        /// 打开一个大富翁4存档文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            // 打开存档文件
            openFileDialog1.Filter = "DAT 文件(*.dat)|*.dat";
            if (openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                openedFile = openFileDialog1.FileName;
                toolStripStatusLabel1.Text = openedFile;
                
                // 读取文件
                fe = new FileEditor();
                fe.read(openedFile);

                toolStripStatusLabel1.Text = openedFile+" 读取完成";
                loadTabs(fe);
            }
        }

        /// <summary>
        /// 加载选项卡
        /// </summary>
        private void loadTabs(FileEditor fe)
        {
            tabControl1.TabPages.Clear();

            // 新建窗体加入到选项卡中  
            eps = new EditPanel[4];


            for(int i=0;i<4;i++)
            {
                EditPanel p1 = new EditPanel();

                p1.FormBorderStyle = FormBorderStyle.None;
                p1.TopLevel = false;
                p1.ControlBox = false;
                p1.Dock = DockStyle.Fill;
                p1.Show();

                TabPage page1 = new TabPage("玩家" + (i + 1));
                p1.Parent = page1;
                tabControl1.TabPages.Add(page1);

                eps[i] = p1;

            }

            // 取得现金存款点券道具
            int[] carr = fe.getCurrencyArray();
            int[] darr = fe.getDepositArray();
            int[] dotArr = fe.getDotArray();
            Daoju[] daojuArr = fe.getDaojuArray();
            Card[] cardArr = fe.getCardArray();

            // 在界面上显示
            for (int i = 0; i < 4; i++)
            {
                eps[i].setCurrency(carr[i].ToString());
                eps[i].setDeposit(darr[i].ToString());
                eps[i].setDot(dotArr[i].ToString());
                eps[i].setDaoju(daojuArr[i]);
                eps[i].setCard(cardArr[i]);
            }
        }

        /// <summary>
        /// 保存打开的大富翁4存档文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            // 检查文件是否打开
            if (openedFile==null)
            {
                toolStripStatusLabel1.Text = "请先打开一个大富翁4存档文件";
                return;
            }

            // 取值
            int[] curArr = new int[4];
            int[] depArr = new int[4];
            int[] dotArr = new int[4];

            for (int i = 0; i < 4; i++)
            {
                curArr[i] = eps[i].getCurrency();
                depArr[i] = eps[i].getDeposit();
                dotArr[i] = eps[i].getDot();
            }

            toolStripStatusLabel1.Text = "从界面取值中..";

            // 设值
            fe.setCurrencyArray(curArr);
            fe.setDepositArray(depArr);
            fe.setDotArray(dotArr);
            fe.setDaoju(eps[0].getDaoju(),eps[1].getDaoju(),eps[2].getDaoju(),eps[3].getDaoju());
            fe.setCard( eps[0].getCards(),eps[1].getCards(),eps[2].getCards(),eps[3].getCards());
            toolStripStatusLabel1.Text = "准备存储...";

            // 存盘
            FileStream fs = new FileStream(openedFile, FileMode.Open);// 读取方式打开，得到流
            fe.saveCurrencyArray(fs);
            fe.saveDepositArray(fs);
            fe.saveDotArray(fs);
            fe.saveDaoju(fs);
            fe.saveCard(fs);
            toolStripStatusLabel1.Text = "存储中...";

            fs.Close();


            toolStripStatusLabel1.Text = "文件 "+openedFile+" 已保存";
        }

        /// <summary>
        /// 检查各个输入项的合法性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (eps != null && eps.Length == 4)
            {
                toolStripStatusLabel1.Text = "开始检查输入项合法性";

                for (int i = 0; i < 4; i++)
                {
                    tabControl1.SelectedIndex = i;

                    if (eps[i].check() == false)
                    {
                        toolStripStatusLabel1.Text = "检查出错";
                        return;
                    }
                }

                toolStripStatusLabel1.Text = "所有输入项均合法";
            }
        }
    }
}
